package com.mmall.controller;

import com.mmall.common.ApplicationContextHelper;
import com.mmall.common.JsonData;
import com.mmall.dao.SysAclModuleMapper;
import com.mmall.exception.ParamException;
import com.mmall.model.SysAclModule;
import com.mmall.param.TestVo;
import com.mmall.util.BeanValidator;
import com.mmall.util.JsonMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * Created by asus on 2018/3/29.
 */
@Controller
@RequestMapping("/test")
@Slf4j
public class TestController {

    @RequestMapping("/hello.json")
    @ResponseBody
    public JsonData hello(){
        log.info("hello permission");
        throw new RuntimeException("permission exception");
        //return JsonData.success("hello permission");
    }

    @RequestMapping("/validate.json")
    @ResponseBody
    public JsonData validate(TestVo vo)throws ParamException{
        log.info("validate");
        try {
            Map<String ,String> map = BeanValidator.validate(vo);
            if(map != null && map.entrySet().size()>0){
                for(Map.Entry<String,String> entry : map.entrySet()){
                    log.info("{}->{}",entry.getKey(),entry.getValue());
                }
            }
        }catch (Exception e){

        }
        //throw new RuntimeException("permission exception");
        //return JsonData.success("hello permission");
        return JsonData.success("test validate");
    }

    @RequestMapping("/validateContext.json")
    @ResponseBody
    public JsonData validateContext(TestVo vo)throws ParamException{
        log.info("validate");
        SysAclModuleMapper sysAclModuleMapper = ApplicationContextHelper.popBean(SysAclModuleMapper.class);
        SysAclModule module = sysAclModuleMapper.selectByPrimaryKey(1);
        log.info(JsonMapper.obj2String(module));
        //throw new RuntimeException("permission exception");
        //return JsonData.success("hello permission");
        BeanValidator.check(vo);
        return JsonData.success("test validate");
    }
}
